﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using MediaPortal.Configuration;
using MediaPortal.GUI.Library;
using System.Threading;

namespace MyNetflix2
{
    public class ImageProcessor
    {
        private delegate void GetImageDelegate(string _URL, string _Id);
        private bool _bannerComplete = false;
        private bool _coverComplete = false;

        public bool BannerComplete
        {
            get { return _bannerComplete; }
        }

        public bool CoverComplete
        {
            get { return _coverComplete; }
        }


        /// <summary>
        /// Thread starter for GetBanner
        /// </summary>
        /// <param name="_backdropURL"></param>
        /// <param name="_backdropId"></param>
        public void asyncGetBanner(string _backdropURL, string _backdropId)
        {
            //--------------------------------------------------------------------------------
            // If the file exists exit.
            //--------------------------------------------------------------------------------
            if (!File.Exists(Config.GetFolder(Config.Dir.Thumbs) + "\\MyNetFlix\\BackDrops\\" + _backdropId + ".jpg"))
            {
                GetImageDelegate bannerImage = new GetImageDelegate(GetBanner);
                bannerImage.BeginInvoke(_backdropURL, _backdropId, new AsyncCallback(BannerCallbackMethod), bannerImage);
            }
        }

        /// <summary>
        /// Thread starter for GetCover
        /// </summary>
        /// <param name="_coverURL"></param>
        /// <param name="_coverId"></param>
        public void asyncGetCover(string _coverURL, string _coverId)
        {
            //--------------------------------------------------------------------------------
            // If the file exists exit.
            //--------------------------------------------------------------------------------
            if (!File.Exists(Config.GetFolder(Config.Dir.Thumbs) + "\\MyNetFlix\\Covers\\" + _coverId + ".jpg"))
            {
                GetImageDelegate coverImage = new GetImageDelegate(GetCover);
                coverImage.BeginInvoke(_coverURL, _coverId, new AsyncCallback(CoverCallbackMethod), coverImage);
            }
        }

        /// <summary>
        /// Get the banner from a web stream
        /// </summary>
        /// <param name="_backdropURL"></param>
        /// <param name="_backdropId"></param>
        public void GetBanner(string _backdropURL, string _backdropId)
        {
            string filenameBD = Config.GetFolder(Config.Dir.Thumbs) + "\\MyNetFlix\\BackDrops\\" + _backdropId + ".jpg";

            //--------------------------------------------------------------------------------
            // Get the backdrop image
            //--------------------------------------------------------------------------------
            if (_backdropURL != "")
            {
                try
                {
                    //--------------------------------------------------------------------------------
                    // Make sure the folder exists
                    //--------------------------------------------------------------------------------
                    if (!Directory.Exists(Config.GetFolder(Config.Dir.Thumbs) + "\\MyNetFlix\\BackDrops"))
                        Directory.CreateDirectory(Config.GetFolder(Config.Dir.Thumbs) + "\\MyNetFlix\\BackDrops");

                    WebClient request = new WebClient();
                    request.DownloadFileAsync(new Uri(_backdropURL), filenameBD);
                }
                catch (Exception ex)
                {
                    Log.Error("My NetFlix - ImageProcessor:GetBanner Error - {0}", ex.Message);
                }
            }
        }

        /// <summary>
        /// Get the cover from a web stream
        /// </summary>
        /// <param name="_coverURL"></param>
        /// <param name="_coverId"></param>
        public void GetCover(string _coverURL, string _coverId)
        {
            string filename = Config.GetFolder(Config.Dir.Thumbs) + "\\MyNetFlix\\Covers\\" + _coverId + ".jpg";

            //--------------------------------------------------------------------------------
            // Get the cover image
            //--------------------------------------------------------------------------------
            if (_coverURL != "")
            {
                try
                {
                    //--------------------------------------------------------------------------------
                    // Make sure the folder exisits first!
                    //--------------------------------------------------------------------------------
                    if (!Directory.Exists(Config.GetFolder(Config.Dir.Thumbs) + "\\MyNetFlix\\Covers"))
                        Directory.CreateDirectory(Config.GetFolder(Config.Dir.Thumbs) + "\\MyNetFlix\\Covers");

                    WebClient request = new WebClient();
                    request.DownloadFileAsync(new Uri(_coverURL), filename);
                }
                catch (Exception ex)
                {
                    Log.Error("My NetFlix - ImageProcessor:GetBanner Error - {0}", ex.Message);
                }
            }
        }

        /// <summary>
        /// Call back to end thread execution
        /// </summary>
        /// <param name="ar"></param>
        private void BannerCallbackMethod(IAsyncResult ar)
        {
            _bannerComplete = true;
            //--------------------------------------------------------------------------------
            // Retrieve the delegate.
            //--------------------------------------------------------------------------------
            GetImageDelegate caller = (GetImageDelegate)ar.AsyncState;

            //--------------------------------------------------------------------------------
            // Call EndInvoke
            //--------------------------------------------------------------------------------
            caller.EndInvoke(ar);
        }

        /// <summary>
        /// Call back to end thread execution
        /// </summary>
        /// <param name="ar"></param>
        private void CoverCallbackMethod(IAsyncResult ar)
        {
            _coverComplete = true;
            //--------------------------------------------------------------------------------
            // Retrieve the delegate.
            //--------------------------------------------------------------------------------
            GetImageDelegate caller = (GetImageDelegate)ar.AsyncState;

            //--------------------------------------------------------------------------------
            // Call EndInvoke
            //--------------------------------------------------------------------------------
            caller.EndInvoke(ar);
        }
    }
}
